package com.party.core.service.crowdfund.impl;

import com.party.common.paging.Page;
import com.party.core.dao.read.crowdfund.CrowfundFollowUpReadDao;
import com.party.core.dao.write.crowdfund.CrowfundFollowUpWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.crowdfund.CrowdfundEventWithMemberPartner;
import com.party.core.model.crowdfund.EventStatistics;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.notify.Event;
import com.party.core.service.crowdfund.ICrowfundFollowUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/***
 * 众筹结束跟进接口实现
 */
@Service
public class CrowfundFollowUpService implements ICrowfundFollowUpService {

    @Autowired
    private CrowfundFollowUpReadDao crowfundFollowReadUpDao;

    @Autowired
    private CrowfundFollowUpWriteDao crowfundFollowUpWriteDao;

    /**
     * 众筹结束跟进初始化方法
     * 众筹结束跟进定时更新数据
     */
    @Override
    public void initializationEventStatistics(int followState) {

        /**查询所有已经截止的主题id*/
        List<Event> EventList = crowfundFollowReadUpDao.selectEventId();
        if(EventList.size()>0){
            for (Event event : EventList) {
                /** 统计退款人数*/
                EventStatistics refundNumberList = crowfundFollowReadUpDao.selectRefundNumber(event.getId());
                if(refundNumberList!=null) {
                    refundNumberList.setEventId(event.getId());
                    EventStatistics eventStatistics = crowfundFollowReadUpDao.selectEventStatisticsByEventId(refundNumberList.getEventId());
                    if (eventStatistics == null) {
                        /**代表可能初始化，也可能更新*/
                        BaseModel.preInsert(refundNumberList);
                        refundNumberList.setFollowState(followState);
                        crowfundFollowUpWriteDao.insert(refundNumberList);
                    } else {
                        eventStatistics.setRefundNumber(refundNumberList.getRefundNumber());
                        crowfundFollowUpWriteDao.update(eventStatistics);
                    }
                }
                /** 统计未退款人数*/
                EventStatistics notRefundNumberList = crowfundFollowReadUpDao.selectNotRefundNumber(event.getId());
                if(notRefundNumberList!=null) {
                    notRefundNumberList.setEventId(event.getId());
                    EventStatistics eventStatistics1 = crowfundFollowReadUpDao.selectEventStatisticsByEventId(notRefundNumberList.getEventId());
                    if (eventStatistics1 == null) {
                        /**代表可能初始化，也可能更新*/
                        BaseModel.preInsert(notRefundNumberList);
                        notRefundNumberList.setFollowState(followState);
                        crowfundFollowUpWriteDao.insert(notRefundNumberList);
                    } else {
                        eventStatistics1.setNotRefundNumber(notRefundNumberList.getNotRefundNumber());
                        crowfundFollowUpWriteDao.update(eventStatistics1);
                    }
                }
                /** 统计转移人数*/
                EventStatistics numberTransfersList = crowfundFollowReadUpDao.selectNumberTransfers(event.getId());
                if(numberTransfersList!=null) {
                    numberTransfersList.setEventId(event.getId());
                    EventStatistics eventStatistics2 = crowfundFollowReadUpDao.selectEventStatisticsByEventId(numberTransfersList.getEventId());
                    if (eventStatistics2 == null) {
                        /**代表可能初始化，也可能更新*/
                        BaseModel.preInsert(numberTransfersList);
                        numberTransfersList.setFollowState(followState);
                        numberTransfersList.setTotalFunds(numberTransfersList.getNumberTransfers());
                        crowfundFollowUpWriteDao.insert(numberTransfersList);
                    } else {
                        eventStatistics2.setNumberTransfers(numberTransfersList.getNumberTransfers());
                        eventStatistics2.setTotalFunds(eventStatistics2.getNotNumberTransfers() + numberTransfersList.getNumberTransfers());
                        crowfundFollowUpWriteDao.update(eventStatistics2);
                    }
                }
                /** 统计未转移人数*/
                EventStatistics notNumberTransfersList = crowfundFollowReadUpDao.selectNotNumberTransfers(event.getId());
                if(notNumberTransfersList!=null) {
                    notNumberTransfersList.setEventId(event.getId());
                    EventStatistics eventStatistics3 = crowfundFollowReadUpDao.selectEventStatisticsByEventId(notNumberTransfersList.getEventId());
                    if (eventStatistics3 == null) {
                        /**代表可能初始化，也可能更新*/
                        BaseModel.preInsert(notNumberTransfersList);
                        notNumberTransfersList.setFollowState(followState);
                        notNumberTransfersList.setTotalFunds(notNumberTransfersList.getNotNumberTransfers());
                        crowfundFollowUpWriteDao.insert(notNumberTransfersList);
                    } else {
                        eventStatistics3.setNotNumberTransfers(notNumberTransfersList.getNotNumberTransfers());
                        eventStatistics3.setTotalFunds(notNumberTransfersList.getNotNumberTransfers() + eventStatistics3.getNumberTransfers());
                        crowfundFollowUpWriteDao.update(eventStatistics3);
                    }
                }
            }
        }
    }



    /**
     * 众筹结束跟进列表
     * @param params
     * @param page
     * @return
     */
    @Override
    public List<Map<String, Object>> endList(CrowdfundEventWithMemberPartner crowdfundEvent,Map<String, Object> params, Page page) {
        return crowfundFollowReadUpDao.endList(crowdfundEvent,params, page);
    }

    /**
     * 众筹结束跟状态改变
     */
    @Override
    public int update(String id, int followState) {
        EventStatistics eventStatistics = new EventStatistics();
        if(followState == 0){
            eventStatistics.setId(id);
            eventStatistics.setFollowState(1);
            crowfundFollowUpWriteDao.update(eventStatistics);
            return 1;
        }
        if(followState == 1){
            eventStatistics.setId(id);
            eventStatistics.setFollowState(2);
            crowfundFollowUpWriteDao.update(eventStatistics);
            return 1;
        }
        return 0;
    }


    /**
     * 众筹结束校正数据
     */
    @Override
    public int revise(String eventId) {
        EventStatistics eventStatistics = crowfundFollowReadUpDao.selectEventStatisticsByEventId(eventId);
        if(eventStatistics==null){
            return 0;
        }
        /** 统计退款人数*/
        EventStatistics refundNumberList = crowfundFollowReadUpDao.selectRefundNumber(eventId);
        /** 统计未退款人数*/
        EventStatistics notRefundNumberList = crowfundFollowReadUpDao.selectNotRefundNumber(eventId);
        /** 统计转移人数*/
        EventStatistics numberTransfersList = crowfundFollowReadUpDao.selectNumberTransfers(eventId);
        /** 统计未转移人数*/
        EventStatistics notNumberTransfersList = crowfundFollowReadUpDao.selectNotNumberTransfers(eventId);
        if(refundNumberList!=null) {
            eventStatistics.setRefundNumber(refundNumberList.getRefundNumber());
        }
        if(notRefundNumberList!=null) {
            eventStatistics.setNotRefundNumber(notRefundNumberList.getNotRefundNumber());
        }
        Integer numberTransfers = 0;
        if(numberTransfersList!=null) {
            numberTransfers = numberTransfersList.getNumberTransfers();
            eventStatistics.setNumberTransfers(numberTransfers);
        }
        Integer notNumberTransfers = 0;
        if(notNumberTransfersList!=null) {
            notNumberTransfers = notNumberTransfersList.getNotNumberTransfers();
            eventStatistics.setNotNumberTransfers(notNumberTransfers);
        }
        eventStatistics.setTotalFunds(notNumberTransfers+numberTransfers);
        crowfundFollowUpWriteDao.update(eventStatistics);
        return 1;
    }

    @Override
    public List<Project> selectEvent(String id) {

        return crowfundFollowReadUpDao.selectEvent(id);
    }

    @Override
    public List<Event> selectEventList() {
        return crowfundFollowReadUpDao.selectEventList();
    }




}
